package com.demo.tools

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.load.resource.bitmap.Downsampler
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.demo.tools.databinding.ActivityMainBinding

class MainActivity : BaseBindingActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
    override fun initData() {
        super.initData()
        ViewCompat.setOnApplyWindowInsetsListener(mBinding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            val lp = mTitleBinding.titleStatusBar.layoutParams
//            if (lp is LinearLayoutCompat.LayoutParams) {
//                lp.height = systemBars.top
//                mTitleBinding.titleStatusBar.layoutParams = lp
//            }
            insets
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
//        setContentView(R.layout.activity_main)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }

        //glide oom优化
        //1.配置合理尺寸（避免加载原图）override(100, 100)
//        Glide.with(this)
//            .load("")
//            .override(100, 100)
//            .into()

        //2.降低内存DecodeFormat.PREFER_RGB_565
//        Glide.with(this)
//            .load("")
//            .override(100, 100)
//            .skipMemoryCache(true)
//            .format(DecodeFormat.PREFER_RGB_565)
//            .into()

        //3.跳过内存缓存skipMemoryCache(true)
//        Glide.with(this)
//            .load("")
//            .override(100, 100)
//            .skipMemoryCache(true)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)
//            .into()

        //4.根据目标尺寸动态采样DownsampleStrategy.CENTER_INSIDE
//        Glide.with(this)
//            .load("")
//            .override(100, 100)
//            .skipMemoryCache(true)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)
//            .downsample(DownsampleStrategy.CENTER_INSIDE)
//            .into()

        //5.使用硬件加速位图Downsampler.ALLOW_HARDWARE_CONFIG
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            Glide.with(this)
//                .load("")
//                .override(100, 100)
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .downsample(DownsampleStrategy.CENTER_INSIDE)
//                .optionalTransform(Downsampler.ALLOW_HARDWARE_CONFIG)
//                .into()
//        }

        //6.RecyclerView滑动优化
//        RecyclerView.SCROLL_STATE_DRAGGING -> Glide.with(this).pauseRequests()
//        RecyclerView.SCROLL_STATE_IDLE -> Glide.with(this).resumeRequests()

        //在页面销毁时释放Glide内存
//        Glide.get(this).clearMemory()

        //绑定生命周期：使用Activity/Fragment的View作为上下文
        //避免非UI线程持有ImageView

//        Glide.with(this)
//            .asDrawable()
//            .load("https://img.imweibo888.com/test/172764/202403/22/17/1711099019113_5739.jpg")
////            .override(400, 400)
//            .diskCacheStrategy(DiskCacheStrategy.NONE)
//            .into(mBinding.img1)


//        val url = "https://img.imweibo888.com/test/172764/202403/22/17/1711099019113_5739.jpg"
        val url = R.mipmap.img_renzheng
        Glide.with(this)
            .asBitmap()
            .load(url)
//            .override(400, 400)
            .format(DecodeFormat.PREFER_RGB_565)
            .skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
//            .thumbnail(Glide.with(this).load(0)) //用其它图片作为缩略图
//            .thumbnail(0.1f)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    Log.d("==Glide==", "onResourceReady: ${resource.width} ${resource.height} ${resource.byteCount * 1f / 1024} ${resource.config}")
                    mBinding.img1.setImageBitmap(resource)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    Log.d("==Glide==", "onLoadCleared: ")
                }
            })
//            .into(object : CustomTarget<Drawable>() {
//                override fun onResourceReady(
//                    resource: Drawable,
//                    transition: Transition<in Drawable>?
//                ) {
//                    Log.d("==Glide==", "onResourceReady: ${resource.minimumWidth} ${resource.minimumHeight}")
//                    mBinding.img1.setImageDrawable(resource)
//                }
//
//                override fun onLoadCleared(placeholder: Drawable?) {
//                    Log.d("==Glide==", "onLoadCleared: ")
//                }
//            })
    }
}